
package cse474_MultiThreadedHTTPServer.Logger;

import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author 120050012
 */
public class LogWindow extends javax.swing.JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Vector<?> threadInfo = null;
    /** Creates new form LogWindow */
    public LogWindow(Vector<?> vector) {
    	this.threadInfo = vector;
    	initComponents();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

    	jScrollPane1 = new javax.swing.JScrollPane();
        threadList = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        responseArea = new javax.swing.JTextArea();
        requestText = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        requestArea = new javax.swing.JTextArea();
        infoLabel = new javax.swing.JLabel();
        refreshButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        threadList.setModel(new javax.swing.AbstractListModel() {
            /**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			String[] strings = { };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        threadList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        threadList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                threadListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(threadList);

        responseArea.setColumns(20);
        responseArea.setEditable(false);
        responseArea.setLineWrap(true);
        responseArea.setRows(5);
        jScrollPane3.setViewportView(responseArea);

        requestText.setEditable(false);
        requestText.setName("null"); // NOI18N

        requestArea.setColumns(20);
        requestArea.setEditable(false);
        requestArea.setLineWrap(true);
        requestArea.setRows(5);
        jScrollPane2.setViewportView(requestArea);

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(infoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                    .addComponent(requestText, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)))
            .addGroup(layout.createSequentialGroup()
                .addComponent(refreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(refreshButton, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(infoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(requestText, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
   
    private void threadListValueChanged(javax.swing.event.ListSelectionEvent evt) {
        int index = ((JList)evt.getSource()).getSelectedIndex();
        ThreadInfo t = (ThreadInfo)threadInfo.get (index);
        this.infoLabel.setText("<html>Started:\t"+t.getDateStart()+"<br>"+"Execution Time:"+t.getExecTime());
        this.requestText.setText(t.getRequestHeader());
        this.requestArea.setText(t.getResponseHeader());
        this.responseArea.setText(t.getResponse());
    }
    
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	System.out.println(threadInfo.size());
        this.threadList.removeAll();
        DefaultListModel lm = new DefaultListModel();
        for(int i=0; i<threadInfo.size(); i++) lm.addElement("Thread #"+((ThreadInfo)threadInfo.get(i)).getThreadId());
        this.threadList.setModel(lm);
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LogWindow(null).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel infoLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton refreshButton;
    private javax.swing.JTextArea requestArea;
    private javax.swing.JTextField requestText;
    private javax.swing.JTextArea responseArea;
    private javax.swing.JList threadList;
    // End of variables declaration

}
